package net.dgg.zz.credential.certificateMatching.controller;

import net.dgg.framework.tac.utils.bean.DggRestResponse;
import net.dgg.framework.tac.utils.web.commmon.DggBaseController;
import net.dgg.iboss.base.util.datatable.DataTableUtils;
import net.dgg.iboss.base.util.datatable.domain.DataTableRequest;
import net.dgg.iboss.base.util.datatable.domain.DataTableResponse;
import net.dgg.tmd.foundation.platform.annotation.AuthOpt;
import net.dgg.tmd.foundation.platform.session.SessionManager;
import net.dgg.tmd.foundation.platform.user.entity.UserEntity;
import net.dgg.zz.api.credential.service.CertificateProductConfigurationService;
import net.dgg.zz.common.constant.CredentialConstant;
import net.dgg.zz.credential.certificateMatching.entity.CertificateMatchingEntity;
import net.dgg.zz.credential.certificateMatching.service.CertificateMatchingService;
import net.dgg.zz.credential.certificateType.entity.CertificateType;
import net.dgg.zz.credential.certificateType.service.CertificateTypeService;
import net.fblock.log.NLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/zs/matching")
public class CertificateMatchingControllor extends DggBaseController {
    @Autowired
    private CertificateMatchingService certificateMatchingService;
    @Autowired
    private CertificateTypeService certificateTypeService;
    @Autowired
    private SessionManager sessionManager;
    @Autowired
    private CertificateProductConfigurationService certificateProductConfigurationService;

    @RequestMapping("/index")
    public String matchingPunishPage(Model model){
        model.addAttribute("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        return "zzsc/certificateMatching/fine_edit_page";
    }

    /**
     * 列表查询
     * @param params
     * @param request
     * @return
     */
    @RequestMapping("/query_list")
    @ResponseBody
    @NLog
    public DataTableResponse queryCertificateMatching(@RequestParam Map<String, Object> params, HttpServletRequest request){
            DataTableRequest r = DataTableUtils.getParam(request);
            params.put("start", (r.getPage() - 1) * r.getLength());
            params.put("limit", r.getLength());
            List<CertificateMatchingEntity> list  =certificateMatchingService.getCertificateMatchingList(params);
            Integer count = certificateMatchingService.count();
            return new DataTableResponse(r.getDraw(), count, count, list, "");
    }


    @RequestMapping("/myProductTable")
    @ResponseBody
    @NLog
    public DataTableResponse queryCertificateMatch(@RequestParam Map<String, Object> params, HttpServletRequest request){
        DataTableRequest r = DataTableUtils.getParam(request);
        params.put("start", (r.getPage() - 1) * r.getLength());
        params.put("limit", r.getLength());
        List<CertificateMatchingEntity> list  =certificateMatchingService.getCertificateMatchingList(params);
        Integer count = certificateMatchingService.count();
        return new DataTableResponse(r.getDraw(), count, count, list, "");
    }












    @RequestMapping(value = "/finecreate_tabpage")
    public String certificateMatchingCreate(Model model) {
        model.addAttribute("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        return "zzsc/certificateMatching/finecreate_tabpage";
    }





    @NLog
    @RequestMapping(value = "/save")
    @ResponseBody
    public DggRestResponse createNewMatchingSave(@RequestParam Map params) {
        try {
            UserEntity userEntity = sessionManager.getCurrentUser();
            Map map = new HashMap();
            map.put("parentId",params.get("certificateType"));
            map.put("generalType",params.get("generalType"));
            if("isAll".equals(params.get("certificateSpecialtys"))) {
                List<Map> mapList = certificateTypeService.getCertificateType(map);
                if(mapList.size()>0) {
                    for (Map map1 : mapList) {
                        Map tempMap = new HashMap();
                        tempMap.put("certificateTypeId",(String) params.get("certificateType"));
                        tempMap.put("certificateSpecialtyId",map1.get("id").toString());
                        tempMap.put("certificateLevel",(String) params.get("certificateLevel"));
                        boolean isExist = certificateMatchingService.isExist(tempMap);
                        if(isExist){
                            CertificateType certificateType = certificateTypeService .findById(Long.valueOf(map1.get("id").toString()));
                            return this.getFailResponse(certificateType.getCertificateName()+"已有扣罚方案，请核对一下！");
                        }
                    }
                }

            }else{
               String ids = params.get("certificateSpecialtys").toString();
                String[] idsArray = ids.split(",");
                for(String idStr: idsArray){
                    Map tempMap = new HashMap();
                    tempMap.put("certificateTypeId",(String) params.get("certificateType"));
                    tempMap.put("certificateSpecialtyId",idStr);
                    tempMap.put("certificateLevel",(String) params.get("certificateLevel"));
                    boolean isExist = certificateMatchingService.isExist(tempMap);
                    if(isExist){
                        CertificateType certificateType = certificateTypeService .findById(Long.valueOf(idStr));
                        return this.getFailResponse(certificateType.getCertificateName()+"已有扣罚方案，请核对一下！");
                    }
                }
            }
            certificateMatchingService.insert(params,userEntity);
            return this.getSuccessResponse("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }

    }

    /**
     * 删除配置
     * @param id
     * @return
     */
    @RequestMapping(value="/delete", method = RequestMethod.POST)
    @ResponseBody
    public DggRestResponse deleteCertificateMatchingById(@RequestParam Long id){
        try{
            certificateMatchingService.deletCertificateMatchingById(id);
            return this.getSuccessResponse("保存成功");
        }catch(Exception e){
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }
    }

    @RequestMapping(value = "/fineedit_tabpage")
    public String certificateMatchingEdit(Model model,Long id) {
        CertificateMatchingEntity certificateMatchingEntity=certificateMatchingService.getCertificateMatchingById(id);
        model.addAttribute("cert",certificateMatchingEntity);
        model.addAttribute("generalType", CredentialConstant.GENERAL_CREDENTIAL);
        return "zzsc/certificateMatching/fineedit_tabpage";
    }



    @NLog
    @RequestMapping(value = "/edit")
    @ResponseBody
    public DggRestResponse editMatchingSave(@RequestParam Map params) {
        try {
            UserEntity userEntity = sessionManager.getCurrentUser();
            Map map = new HashMap();
            map.put("parentId",params.get("certificateType"));
            map.put("generalType",params.get("generalType"));
            if("isAll".equals(params.get("certificateSpecialtys"))) {
                List<Map> mapList = certificateTypeService.getCertificateType(map);
                if(mapList.size()>0) {
                    for (Map map1 : mapList) {
                        Map tempMap = new HashMap();
                        tempMap.put("certificateTypeId",(String) params.get("certificateType"));
                        tempMap.put("certificateSpecialtyId",map1.get("id").toString());
                        tempMap.put("certificateLevel",(String) params.get("certificateLevel"));
                        tempMap.put("id",params.get("id"));
                        boolean isExist = certificateMatchingService.isExist(tempMap);
                        if(isExist){
                            CertificateType certificateType = certificateTypeService .findById(Long.valueOf(map1.get("id").toString()));
                            return this.getFailResponse(certificateType.getCertificateName()+"已有扣罚方案，请核对一下！");
                        }
                    }
                }

            }else{
                String ids = params.get("certificateSpecialtys").toString();
                String[] idsArray = ids.split(",");
                for(String idStr: idsArray){
                    Map tempMap = new HashMap();
                    tempMap.put("certificateTypeId",(String) params.get("certificateType"));
                    tempMap.put("certificateSpecialtyId",idStr);
                    tempMap.put("certificateLevel",(String) params.get("certificateLevel"));
                    tempMap.put("id",params.get("id"));
                    boolean isExist = certificateMatchingService.isExist(tempMap);
                    if(isExist){
                        CertificateType certificateType = certificateTypeService .findById(Long.valueOf(idStr));
                        return this.getFailResponse(certificateType.getCertificateName()+"已有扣罚方案，请核对一下！");
                    }
                }
            }
            certificateMatchingService.updateCertificateMatching(params,userEntity);
            return this.getSuccessResponse("保存成功");
        } catch (Exception e) {
            e.printStackTrace();
            return this.getFailResponse(e.getMessage());
        }

    }



}
